package com.example.springboot.service.impl;
import cn.hutool.core.date.DateUtil;
import com.example.springboot.mapper.LocationMapper;
import com.example.springboot.pojo.Location;
import com.example.springboot.service.LocationRecordService;
import com.example.springboot.mapper.LocationRecordMapper;
import com.example.springboot.pojo.LocationRecord;
import com.example.springboot.utils.ThreadLocalUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LocationRecordServiceImpl implements LocationRecordService {

    @Autowired
    private LocationRecordMapper locationRecordMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Override
    public void create(LocationRecord locationRecord) {
        locationRecord.setNumber(DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        locationMapper.updateBalance(locationRecord.getLocationId());
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        locationRecord.setUserId(userId);
        locationRecordMapper.create(locationRecord);
    }

    @Override
    public void delete(Integer id) {
        locationRecordMapper.delete(id);
    }

    @Override
    public List<LocationRecord> findAll() {
        return locationRecordMapper.findAll();
    }

    @Override
    public List<LocationRecord> fingByNumber(String numeber) {
        return locationRecordMapper.fingByNumber(numeber);
    }


    @Override
    public List<LocationRecord> findByLocationId(Integer locationId) {
        return locationRecordMapper.findByLocationId(locationId);
    }

    @Override
    public List<LocationRecord> findByUserId( ) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return locationRecordMapper.findByUserId(userId);
    }

    @Override
    public void update(Integer id) {
        locationRecordMapper.update(id);
    }
    @Override
    public PageInfo<LocationRecord> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LocationRecord> list = locationRecordMapper.findAll();
        return PageInfo.of(list);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            locationRecordMapper.delete(id);
        }
    }

    @Override
    public void updateByAdmin(Integer id) {
        locationRecordMapper.updateByAdmin(id);
    }

    @Override
    public void updateByUser(Integer id) {
        locationRecordMapper.updateByUser(id);
    }
}
